// 购物车结算页面
let price
var goods = []
let UserOpenid
let date =''
let userID = ''
let sellerID = ''
let sellerNAME = ''
let goodName = ''
let goodPrice = ''
let goodimg = ''
// let orderNum 
var OrderInformation = {UserOrder,UserOpenid}
var Order = []
let UserOrder = []
Page({
  data: {
   list:[],
   Date:null
  },
  onLoad(options) {
    //获取时间
    var myDate = new Date();
    var mytime=myDate.toLocaleTimeString();
    date=(myDate.getFullYear()+'年'+(myDate.getMonth()+1)+'月'+myDate.getDate()+'日'+mytime)
    this.setDATA()
    wx.cloud.callFunction({
      name:'getOPENID'
    })
    .then(res=>{
      UserOpenid = res.result.openid
    })
    price = parseInt(options.amount)
    console.log('总价price:',price)
    this.setData({
      list:JSON.parse(decodeURIComponent(options.array))
    })
    goods = JSON.parse(decodeURIComponent(options.array))
    console.log('goods:',goods)
  },
  tianjia(){
    this.generateAllOrder(goods)
  },
  setDATA(){
    this.setData({
      Date:date
    })
  },
//遍历生成所有订单
generateAllOrder(goods){
  let wanbi = false
  for (let AA = 0; AA < goods.length; AA++) {
    const good = goods[AA];
    sellerID = good.openID
    sellerNAME = good.NAME
    goodName = good.name
    goodPrice = good.price
    goodimg = good.imgpath[0]
    this.OrderInformation(AA)
    console.log('添加订单'+[AA]+'成功')

  }
  //订单长度获取有问题
  // console.log('Order为',Order)
  // console.log('Order长度为',Order.length)
  // console.log('UserOrder为',UserOrder)
  // console.log('goods.length为',goods.length)
  // console.log('UserOrder长度为',UserOrder.length)
  // this.addUserOrder(goods.length,UserOrder)
},
// 生成订单
OrderInformation(index){
let orderNum = Math.floor(Math.random() * 1000) + new Date().getTime() + Math.floor(Math.random() * 1000)
 let state = '待交易'
 wx.cloud.database().collection('Order-information').add({
   data:{
    orderNum:orderNum,
    sellerID:sellerID,
    userID:userID,
    goodName:goodName,
    goodPrice:goodPrice,
    sellerNAME:sellerNAME,
    goodimg:goodimg,
    date:date,
    state:state
   }
})
 .then(res=>{
   console.log('订单信息生成成功：',res)
   var OrderIN = {orderNum,sellerID}
   Order[index] = OrderIN
   UserOrder[index] = orderNum
   console.log('订单信息添加数组成功：',Order)
  //  添加订单数据
   
 })
 .catch(err=>{
  console.log('订单信息生成失败：',err)
})
},
// 添加用户订单   添加订单云函数不适合多次调用，需要再写一个
addUserOrder(NUM,Order){
  let dbcw = wx.cloud.database().collection('All-Orders').where({openid:UserOpenid})
  dbcw.get()
  .then(res=>{
    let NotNUM = 0
    NotNUM = res.data[0].Not.NotNum
    NotNUM = NotNUM+NUM
    console.log('更新后NotNUM：',NotNUM)
    let AllOrder = Order
    // let SellNUM = 0
    // SellNUM = res.data[0].Sell.SellNum
    // console.log('用户订单信息获取成功：',res)
    // console.log('NotNum获取：',NotNUM)
      wx.cloud.callFunction({
        name:'addCartOrder',
        data:{
         Num:NotNUM,
         orderNum:AllOrder,
        }
      })
      .then(res=>{
        console.log('购买者订单添加成功：',res)
      })
      .catch(err=>{
       console.log('购买者订单添加失败：',err)
     })
  })
  .catch(err=>{
    console.log('用户订单信息渲染失败：',err)
  })
},
  //调微信支付
  submitpay() {
    price = price*100
    let that = this;
    wx.cloud.callFunction({
        name: "pay",
        data: {
            money:price
        },
        success(res) {
            console.log("提交成功", res.result)
            that.pay(res.result)
        },
        fail(res) {
            console.log("提交失败", res)
        }
    })
},
pay(payData) {
  //官方标准的支付方法
  let that = this
  wx.requestPayment({
      timeStamp: payData.timeStamp,
      nonceStr: payData.nonceStr,
      package: payData.package, //统一下单接口返回的 prepay_id 格式如：prepay_id=***
      signType: 'MD5',
      paySign: payData.paySign, //签名
      success(res) {
          console.log("支付成功", res)
          that.generateAllOrder(goods)
      },
      fail(res) {
          console.log("支付失败", res)
      },
      complete(res) {
          console.log("支付完成", res)
      }
  })
},
  quxiao(){
    wx.navigateBack({
      delta: 1
    })
  }
})